const { defineConfig } = require('@vue/cli-service')
// noinspection NodeCoreCodingAssistance
const path = require('path')
const proxyOptions = require('./proxy')

const resolve = (dir) => path.resolve(__dirname, dir)
const IS_PRO = process.env.NODE_ENV === 'production'

module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: true,
  productionSourceMap: false,
  devServer: {
    proxy: proxyOptions
  },
  chainWebpack: (config) => {
    // 配置SVG雪碧图
    config.module.rule('svg').exclude.add(resolve('src/assets/icons')).end()
    config.module
      .rule('svg-icon')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({ symbolId: 'icon-[name]' })
      .end()

    config.when(IS_PRO, (cf) => {
      // 第三方依赖拆包
      cf.optimization.splitChunks({
        chunks: 'all', // 这表明将选择哪些块进行优化，有效值为all，async和initial
        automaticNameDelimiter: '-', // 生成名称的分隔符,默认~
        cacheGroups: {
          ElementPlus: {
            test: /[\\/]node_modules[\\/](element-plus)[\\/]/,
            name: 'element-plus',
            priority: 0
          }
        }
      })
    })
  }
})
